/**
 * @param {number[]} prices
 * @return {number}
 */
// var maxProfit = function (prices) {
//   if (prices.length == 0) return 0
//   let profit = 0, valley = prices[0], peak = prices[0]
//   let i = 0;
//   while (i < prices.length - 1) {
//     while (prices[i] >= prices[i + 1] && prices[i]) {
//       i++
//     }
//     valley = prices[i]
//     while (prices[i] <= prices[i + 1] && i < prices.length - 1) {
//       i++
//     }
//     peak = prices[i]
//     profit += peak - valley
//   }
//   return profit
// };

// 贪心算法
var maxProfit = function (prices) {
  if (prices.length == 0) return 0
  let profit = 0
  for (let i = 0; i < prices.length - 1; i++)  if (prices[i] < prices[i + 1]) profit += prices[i + 1] - prices[i]
  return profit
};
console.log(maxProfit([1,2,3,4,5]));